﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution227
    {
        public int Calculate1(string s)//判断条件太多太乱
        {
            HashSet<char> set = new HashSet<char>();

            set.Add('+');
            set.Add('-');
            set.Add('*');
            set.Add('/');
            Stack<char> stackOpe = new Stack<char>();
            Stack<int> stackNum = new Stack<int>();

            int preNum = 0;
            int len = s.Length;

            //先处理乘除
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];

                if (i == len - 1)
                {
                    if (char.IsDigit(c))
                    {
                        int curNum = c - '0';
                        preNum = preNum * 10 + curNum;
                    }
                    if (stackOpe.Count > 0)
                    {
                        if (stackOpe.Peek() == '*' || stackOpe.Peek() == '/')
                        {
                            //结算一下

                            int num = stackNum.Pop();//前面的
                                                     //这个符号是乘还是除
                            preNum = MyCal(stackOpe.Pop(), num, preNum);
                        }
                    }
                    stackNum.Push(preNum);
                }
                else
                {
                    if (set.Contains(c))//是符号或者最后一个
                    {
                        if (stackOpe.Count > 0)
                        {
                            if (stackOpe.Peek() == '*' || stackOpe.Peek() == '/')
                            {
                                //结算一下

                                int num = stackNum.Pop();
                                //这个符号是乘还是除
                                preNum = MyCal(stackOpe.Pop(), preNum, num);
                            }
                        }

                        stackOpe.Push(c);

                        stackNum.Push(preNum);
                        preNum = 0;
                    }
                    else if (c == ' ')
                    {
                    }
                    else
                    {
                        int curNum = c - '0';
                        preNum = preNum * 10 + curNum;
                    }
                }
            }

            preNum = stackNum.Pop();
            while (stackOpe.Count > 0)
            {
                preNum = MyCal(stackOpe.Pop(), preNum, stackNum.Pop());
            }

            return preNum;
        }

        public int MyCal(char ch, int a, int b)
        {
            char add = '+';

            if (ch - add == 0)
            {
                Console.WriteLine("a + b");
                return a + b;
            }
            else if (ch - add == 2)
            {
                Console.WriteLine("a - b");
                return a - b;
            }
            else if (ch - add == -1)
            {
                Console.WriteLine("a * b");
                return a * b;
            }
            else
            {
                Console.WriteLine("a / b");
                return a / b;
            }
        }

        public int Calculate(string s)//判断条件太多太乱，使用官方思想
        {
            HashSet<char> set = new HashSet<char>();

            Stack<int> stack = new Stack<int>();

            int preNum = 0;
            int len = s.Length;
            char preSign = '+';//默认，这样第一个数字直接入栈

            //先处理乘除
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                if (char.IsDigit(c))
                {
                    int curNum = c - '0';
                    preNum = preNum * 10 + curNum;
                }

                //不是数字不是空格，就是操作符。执行一次操作。到最后一位也要执行一次
                if (!char.IsDigit(c) && c != ' ' || i == len - 1)
                {
                    switch (preSign)
                    {
                        case '+':
                            stack.Push(preNum);//入栈
                            break;

                        case '-':
                            stack.Push(-preNum);//负数入栈
                            break;

                        case '*':
                            stack.Push(stack.Pop() * preNum);
                            break;

                        default:
                            stack.Push(stack.Pop() / preNum);
                            break;
                    }
                    //更新符号
                    preSign = c;
                    preNum = 0;
                }
            }

            int res = 0;
            while (stack.Count > 0)
            {
                res += stack.Pop();
            }

            return res;
        }
    }

    internal class Program227
    {
        private static void Main227(string[] args)
        {
            // string s = "3+2*2";
            string s = "0";
            //string s = " 3/2 ";

            Solution227 Solution = new Solution227();
            int res = Solution.Calculate(s);
            Console.WriteLine(res);
        }
    }
}